package at.fhj.itm11.forum.service.stubs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import at.fhj.itm11.forum.data.TopicDAO;
import at.fhj.itm11.forum.model.Topic;

public class TopicDAOStub implements TopicDAO {

	private Set<Topic> repo = new HashSet<Topic>();

	@Override
	public Topic findById(Long id) {
		for (Topic topic : repo) {
			if (topic.getId().equals(id)) {
				return topic;
			}
		}

		return null;
	}

	@Override
	public List<Topic> retrieveAllTopicsOrderedByTitle() {
		ArrayList<Topic> result = new ArrayList<Topic>(repo);
		Collections.sort(result, new Comparator<Topic>() {

			@Override
			public int compare(Topic o1, Topic o2) {
				return o1.getTitle().compareTo(o2.getTitle());
			}

		});

		return result;
	}

	@Override
	public void register(Topic topic) {
		repo.add(topic);
	}

	public Set<Topic> getRepo() {
		return repo;
	}

	public void setRepo(Set<Topic> repo) {
		this.repo = repo;
	}

}
